package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.OperateLog;
import org.example.system.entity.Payment;
import org.example.system.entity.Payment;
import org.example.system.service.LoginLogService;
import org.example.system.service.OperateLogService;
import org.example.system.service.PaymentService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 缴费控制层
 */
@RestController
@RequestMapping("/admin/payment")
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private OperateLogService operateLogService;

    //操作日志对象
    private OperateLog operateLog = new OperateLog("缴费管理");

    /**
     * 缴费
     * @param payment
     * @return
     */
    @PostMapping("/pay")
    public R pay(@RequestBody Payment payment,HttpServletRequest request){
        payment.setPageNum(1L);
        payment.setPageSize(10L);
        IPage<Payment> page = paymentService.selectPage(payment);
        //存在缴费，直接缴费，不存在则先创建
        if (page.getTotal() == 1){
            Payment exitPayment = page.getRecords().get(0);
            payment.setId(exitPayment.getId());
            payment.setWaterFee(exitPayment.getWaterFee() + payment.getWaterFee());
            payment.setElectricFee(exitPayment.getElectricFee() + payment.getElectricFee());
            operateLog.setUsername(payment.getUpdateBy());
            paymentService.update(payment);
        }else{
            operateLog.setUsername(payment.getCreateBy());
            paymentService.add(payment);
        }
        operateLog.setInfo("缴费成功");
        operateLogService.add(operateLog,request);
        return R.ok(page);
    }

    /**
     * 修改
     * @param payment
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Payment payment, HttpServletRequest request){
        operateLog.setUsername(payment.getUpdateBy());
        log.info("--------- PaymentController | update | input : {}", JSON.toJSON(payment));
        if(null == payment.getId() || payment.getId() <= 0){
            operateLog.setInfo("参数错误:ID未传入");
            operateLogService.add(operateLog,request);
            return R.fail("参数错误:ID未传入");
        }
        int row = paymentService.update(payment);
        if(row <= 0){
            operateLog.setInfo("修改失败");
            operateLogService.add(operateLog,request);
            return  R.fail("修改失败");
        }
        operateLog.setInfo("修改成功");
        operateLogService.add(operateLog,request);
        return R.ok("修改成功");
    }

    /**
     * 按条件分页查询
     * @param payment
     * @return
     */
    @GetMapping("/list")
    public R list(Payment payment){
        IPage<Payment> page = paymentService.selectPage(payment);
        log.info("------------ PaymentController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 根据id查询缴费数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- PaymentControl | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(paymentService.getOne(id));
    }
}
